package com.lyw.controller;

import com.lyw.entity.User;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Log4j2
@Controller
public class FormController {

  @GetMapping("/form-layouts.html")
  public String formlayouts() {
    return "form-layouts";
  }

  @GetMapping("/basic-form.html")
  public String basicform() {
    //int i = 10 / 0;
    return "basic-form";
  }

  @PostMapping("/uploadfile")
  public String upload(
          User user,
          @RequestPart("header") MultipartFile header,
          @RequestPart("photos") MultipartFile[] photos
  ) throws IOException {
    /*log.info("username={},passwd={},header={},photos={}",
            user.getUsername(), user.getPasswd(), header.getSize(), photos.length);*/
    if (header.isEmpty())
      return "上传失败！";
    header.transferTo(new File("/Users/liyawei/idea_workspace/review-springboot2-atguigu/springboot-systemos/src/main/resources/picture/header/" + header.getOriginalFilename()));
    for (MultipartFile photo : photos) {
      if (photo.isEmpty())
        return "上传失败！";
      photo.transferTo(new File("/Users/liyawei/idea_workspace/review-springboot2-atguigu/springboot-systemos/src/main/resources/picture/photos/" + photo.getOriginalFilename()));
    }



    return "/main.html";

  }
}
